/* Copyright 2008 dnAnalytics Project.
 *
 * Contributors to this file:
 * Marcus Cuda - http://marcus.cuda.net
 *
 * This file is part of dnAnalytics.  dnAnalytics is licensed under the 
 * Microsoft Public License. See License.txt for a complete copy of the
 * license.
 */

namespace dnAnalytics.LinearAlgebra
{
    /// <summary>
    /// Computes the Householder transformation of a vector.
    /// </summary>
    public partial class HouseholderTransformation
    {
        private Vector mVector;
        private double mBeta;

        /// <summary>
        /// The Householder reflection as a vector.  The vector is normalized so that V[0] = 1.
        /// </summary>
        public Vector ReflectionVector()
        {
            return mVector.Clone();
        }

        /// <summary>
        /// The Householder reflection as a matrix.
        /// H = I - ( (2/v^Tv)*(vv^T)
        /// where v is Householder.Vector.
        /// </summary>
        public Matrix ReflectionMatrix()
        {
            Matrix I = mVector.CreateMatrix(mVector.Count, mVector.Count);
            for (int i = 0; i < mVector.Count; i++)
            {
                I[i,i] = 1.0;
            }
            double constant = 2.0 / mVector.DotProduct(mVector);
            Matrix vt = mVector.Multiply(mVector);
            return I - (constant*vt);
        }

        /// <summary>
        /// The transformation of the original vector. 
        /// transformation = H*v
        /// where H = Householder.Matrix and v is the original vector.
        /// </summary>
        /// <remarks>All elements except the first are zero.</remarks>
        public Vector Transformation()
        {
            Vector ret = new DenseVector(mVector.Count);
            ret[0] = mBeta;
            return ret;
        }
    }
}